package com.neu.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.neu.common.AutoLog;
import com.neu.common.Result;
import com.neu.entity.Admin;
import com.neu.entity.Book;
import com.neu.entity.Params;
import com.neu.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.generator.formatter.ListTemplateFormatter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.hutool.core.util.ReUtil.findAll;

@CrossOrigin
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/search")
    public Result findBySearch(Params params){
        PageInfo<Book> info = bookService.findBySearch(params);
        return Result.success(info);
    }

    @PostMapping
    @AutoLog("修改图书信息")
    public Result add(@RequestBody Book book){
        if(book.getId() == null){
            bookService.add(book);
        }else{
            bookService.update(book);
        }

        return Result.success();
    }

    @DeleteMapping("/{id}")
    @AutoLog("删除图书信息")
    public Result delete(@PathVariable Integer id){
        bookService.delete(id);
        return Result.success();
    }

    @GetMapping("/echarts/bie")
    public Result bie(){
        //查询出所有图书
        List<Book> list= bookService.findAll();
        Map<String,Long> collect = list.stream()
                .filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))
                .collect(Collectors.groupingBy(Book::getTypeName,Collectors.counting()));
        //最后返回给前端的数据结构
        List<Map<String,Object>> mapList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(collect)){
            for(String key: collect.keySet()){
                Map<String,Object> map = new HashMap<>();
                map.put("name",key);
                map.put("value",collect.get(key));
                mapList.add(map);
            }
        }
        return Result.success(mapList);
    }

    @GetMapping("/echarts/bar")
    public Result bar(){
        //查询出所有图书
        List<Book> list= bookService.findAll();
        Map<String,Long> collect = list.stream()
                .filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))
                .collect(Collectors.groupingBy(Book::getTypeName,Collectors.counting()));
        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(collect)){
            for(String key: collect.keySet()){
                xAxis.add(key);
                yAxis.add(collect.get(key));
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("xAxis",xAxis);
        map.put("yAxis",yAxis);

        return Result.success(map);
    }

}
